<?php
App::uses('AppController', 'Controller');

/**
 * Banners Controller
 *
 * @property Banner $Banner
 */
class BannersController extends AppController {
	public function beforeFilter() {
		parent::beforeFilter();
	}

	public function admin_index(){
	}
	
	public function admin_edit($id = null) {
		$this->Banner->id = $id;
		if (!$this->Banner->exists()) {
			throw new NotFoundException('Invalid banner');
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Banner->save($this->request->data)) {
				$this->Session->setFlash('The banner has been saved');
				$url = $this->Session->read('Controller.toUrl');
				$this->redirect($url);
			} else {
				$this->Session->setFlash('The banner could not be saved. Please, try again.');
			}
		} else {
			$options = array('conditions' => array('Banner.' . $this->Banner->primaryKey => $id));
			$this->request->data = $this->Banner->find('first', $options);
		}
	}
	
	public function ajax_index(){
		$this->paginate = array(
			'recursive' => 0,
			'limit' => 10,
			'order' => array('Banner.created' => 'DESC'),
		);
		$this->set('banners', $this->paginate());
	}
	
	public $components = array('Resize');
	public function ajax_upload(){
		if(!empty($this->request->data['Upload'])){ 
			$images = $this->request->data['Upload'];
			$upload = array();
			foreach ($images as $i => $image){
				if(!empty($image['tmp_name']) && is_uploaded_file($image['tmp_name'])){
					$fileName = md5(time().$image['name']).'.'.$this->getFileExtension($image['name']);
					CakeLog::write('debug', 'uploaded ' . $fileName);
					
					if(move_uploaded_file($image['tmp_name'], IMAGES.'uploads'.DS.$fileName)){
						CakeLog::write('debug', 'copied to ' . IMAGES.'uploads'.DS.$fileName);
						$url1 = $this->Resize->resize($fileName, 75, 75, 50, 1, 'thumbs');
						$url2 = $this->Resize->resize($fileName, 960, 640, 100, 0, 'fulls');
						$upload[$i]['url1'] = $url1;
						$upload[$i]['url2'] = $url2;				 
					}
				}	
			}
			$this->set('images', $upload);
		}
	}
	
	public function api_add(){
		$href = $this->request->data['Banner']['href'];
		if(!empty($this->request->data['Attach'])){
			$photos = $this->request->data['Attach'];
			$count = 0;
			foreach($photos as $photo){
				list($url1, $url2) = split("\|", $photo, 2);
				$this->Banner->create();
				if($this->Banner->save(array(
					'href' => $href,
					'url1' => $url1,
					'url2' => $url2)))
					$count ++;
			}
			$error = 0;
			$message = $count.' banners have been saved.';
		}
		else{
			$error = -1;
			$message = 'The banners could not be saved.';
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_status($id = null){
		$error = -1;
		$this->Banner->id = $id;
		if (!$this->Banner->exists()) {
			$message = 'Invalid banner';
		}
		else{		
			if($this->Banner->updateAll(
			array('Banner.status' => '1 - Banner.status', 'modified' => 'NOW()'),
			array('Banner.id' => $id))){
				$error = 0;
				$message = 'Status has been changed';
			}
			else{
				$error = 1;
				$message = 'Status could not be changed';					
			}
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
	
	public function api_delete($id = null){
		$error = -1;
		$this->Banner->id = $id;
		if (!$this->Banner->exists()) {
			$message = 'Invalid banner';
		}
		if ($this->Banner->delete()) {
			$error = 0;
			$message = 'Banner deleted';
		}
		else{
			$error = 1;
			$message = 'Banner was not deleted';			
		}
		$this->set('error', $error);
		$this->set('data', $message);
	}
}